const {
  $util,
} = getApp();
Page({
  data: {
    imgs: [],
    check: 0,
    record: '',
    uploading: false,
    offcontent: '',
    comment: '',
    score: '',
    nav: [{
      title: '上传笔记',
      id: 0,
    }, {
      title: '培训评价',
      id: 1,
    }],
    hasMore: true,
    page: 1,
    navIndex: 0,
    commentList: [],
    picture: [],
    showMore: false,
    showMore2: false,
    imgUrl: $util.getImgUrl(),
    ifShowNote: true,
  },
  onLoad(options) {
    this.setData(options);
    this.loadDetail();
    this.ifShowNote();
  },
  onShow() {
    let wxData = wx.getStorageSync('qyUserInfo') || {};
    this.setData(wxData);
    this.setData({
      isSaaS: wx.getStorageSync('isSaaS'),
    });
  },
  getMore() {
    this.setData({
      showMore: !this.data.showMore,
    });
  },
  getMore2() {
    this.setData({
      showMore2: !this.data.showMore2,
    });
  },
  /**
   * 线下培训详情
   */
  loadDetail() {
    wx.showLoading({
      title: '加载中',
    });
    $util.request({
      url: 'entry/wxapp/edu.offlinetrain.details',
      data: {
        id: this.data.id,
      },
      success: (res) => {
        console.log(res)
        const list = res.list;
        res.list.lastimg && res.list.lastimg.length && (list.imgs = list.lastimg);
        this.setData(list);
        this.setData({
          [`nav[0].title`]: Number(list.status) !== 3 && Number(this.data.navIndex) === 0 ? '上传笔记' : '笔记',
        });
        wx.hideLoading();
      },
    });
  },
  loadCommentDetail() {
    $util.request({
      url: 'entry/wxapp/edu.offlinetrain.commentlist',
      data: {
        id: this.data.id,
        page: this.data.page,
      },
      success: (res) => {
        let commentList = res.list;
        if (this.data.page > 1) {
          commentList = [...this.data.commentList, ...commentList];
        }
        this.setData({
          commentList: commentList,
          page: this.data.page + 1,
          hasMore: res.list.length > 0,
        });
      },
    });
  },
  changeNav(e) {
    console.log(e)
    this.setData({
      page: 1,
      navIndex: e.detail.obj.id,
      hasMore: true,
    }, () => {
      if (e.detail.obj.id === 1) {
        this.loadCommentDetail();
      }
    });
  },
  /**
   * 培训记录
   */
  bindIpVal(e) {
    const id = e.currentTarget.dataset.id;
    const m = {};
    m[id] = e.detail.value;
    this.setData(m);
  },
  /**
   * 发布评论
   */
  submitCommont() {
    let { score, comment } = this.data;
    if (score === '') {
      wx.showToast({
        title: '请选择培训评分',
        icon: 'none',
      });
      return;
    }
    if (!(/(^[1-5]$)/.test(Number(score)))) {
      wx.showToast({
        title: '请选择正确的培训评分，评分为1~5整数',
        icon: 'none',
      });
      return;
    }
    if (comment === '') {
      wx.showToast({
        title: '请填写培训评价',
        icon: 'none',
      });
      return;
    }
    this.submit('submit');
  },

  // 隐藏上传笔记
  ifShowNote() {
    $util.request({
      url: 'entry/wxapp/edu.offlinetrain.offlist',
      data: {},
      success: (res) => {
        for (var i in res.list) {
          // 判断是否隐藏上传笔记
          if (res.list[i].upload_notes == 0) {
            this.setData({
              ifShowNote: false,
              nav: [{
                title: '培训评价',
                id: 1,
              }],
              page: 1,
              navIndex: 1,
              hasMore: true,
            }, () => {
              this.loadCommentDetail();
            })
          } else if (res.list[i].upload_notes == 1) {
            this.setData({
              ifShowNote: true
            })
          }
        }
      }
    })
  },

  /**
   * 上传
   */
  submit(param) {
    if (this.data.offcontent.trim() === '' && param !== 'submit') {
      wx.showToast({
        title: '请填写培训笔记',
        icon: 'none',
      });
      return;
    }
    this.setData({
      uploading: true,
    });
    $util.request({
      url: `entry/wxapp/${param === 'submit' ? 'edu.offlinetrain.upevaluate' : 'edu.offlinetrain.upnotes'}`,
      data: {
        id: this.data.id,
        image: this.data.imgs,
        content: this.data.offcontent,
        comment: this.data.comment,
        score: this.data.score,
      },
      success: (res) => {
        this.setData({
          uploading: false,
        });
        wx.showToast({
          title: param === 'submit' ? '发表成功' : '上传成功',
          icon: 'success',
          duration: 1e3,
        });
        setTimeout(() => {
          if (param === 'submit') {
            this.setData({
              score_check: 2,
              page: 1,
            }, () => {
              this.loadCommentDetail();
            });
          } else {
            wx.navigateBack();
          }
        }, 1e3);
      },
      fail: (err) => {
        wx.showToast({
          title: err.message,
          icon: 'none',
          duration: 1e3,
        });
        this.setData({
          uploading: false,
        });
      },
    });
  },
  /**
   * 获取最新的 待提交的图片列表
   * @param {object} e 来自组件的数据
   * @param {string[]} e.detail
   * @return
   */
  imgsOnChange(e) {
    this.setData({
      imgs: e.detail,
    });
  },
  lastimgOnchange(e) {
    this.setData({
      picture: e.detail,
    });
  },
  onReachBottom() {
    this.data.navIndex === 1 && this.data.page > 1 && this.data.hasMore && this.loadCommentDetail();
  },

  setRate(e) {
    let { score } = e.target.dataset;
    let { navIndex } = this.data;
    Number(navIndex) === 1 && this.setData({
      score,
    });
  },
});